<?php

namespace App\Http\Controllers\Auth;

use Auth;
use Session;
use Illuminate\Http\Request;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

use App\Models\User;
use App\Http\Controllers\Controller;
use App\Http\Requests\Auth\RegisterRequest;
use App\Http\Requests\Auth\LoginRequest;

class AuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;
    
    /*
     * 设置登录成功后，重定向路径
     */
    protected $redirectPath = '/home';
    
    /*
     * 设置登录失败后，重定向路径
     */
    protected $loginPath = '/login';
    
     /*
     * 设置登录失败后，重定向路径
     */
    protected $user_model;

    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct(User $user)
    {
        $this->user_model = new $user;
        $this->middleware('guest', ['except' => 'getLogout']);
    }

    /**
     * 显示登录表单页面
     *
     * @return view
     */
    public function getLogin()
    {
        $result = array();
        return view('auth.login',['data' => $result]);
    }
    
    /**
     * 显示注册表单页面
     *
     * @return view
     */
    public function getRegister()
    {
        $result = array();
        return view('auth.register',['data' => $result]);
    }
	
	/**
     * 退出登录操作
     *
     * @return view
     */
    public function getLogout()
    {
        Auth::logout();
		//Session::pull('users');
		return redirect()->intended($this->redirectPath());	
    }

    /**
     * 验证登录用户信息
     *
     * @param  \Illuminate\Http\Request  $request
     * @return User
     */
    public function authLogin(LoginRequest $request)
    {
        $credentials = $this->getCredentials($request);
		
		/*
		 * Auth::attempt使用报错
		 * 参考:https://phphub.org/topics/1151
		 */
		
        if (!Auth::attempt($credentials, $request->has('remember'))) {
            return redirect('/login')
					->with('error_message', 'login fail!')
					->withInput($request->only('email'));
        }
		
		//$request->session()->put('users', Auth::user());

		return redirect()->intended($this->redirectPath());	
    }
    
    /**
     * 保存注册用户信息
     *
     * @param  \Illuminate\Http\Request  $request
     * @return User
     */
    public function storeRegister(RegisterRequest $request)
    {
        $request = $request->all();
        
        $user = $this->user_model;
        $user->nickname = $request['nickname'];
        $user->email = $request['email'];
        $user->phone = $request['phone'];
        $user->password = bcrypt($request['password']);
        $result = $user->save();
        
        if($result){
            return redirect('/login')->with('message', '欢迎注册，好好玩耍!');
        }else{
            return redirect('/register')->with('message', '注册失败，请重新注册。')->withErrors($validator)->withInput();
        }
    }
}
